package com.kk.xhr.admin.biz.impl;

import com.kk.xhr.admin.biz.IPermissionBizService;
import com.kk.xhr.admin.controller.request.PermissionAddRequest;
import com.kk.xhr.admin.controller.request.PermissionModifyRequest;
import com.kk.xhr.admin.controller.vo.Meta;
import com.kk.xhr.admin.controller.vo.PermissionTreeVO;
import com.kk.xhr.common.pojo.BinaryEnum;
import com.kk.xhr.model.dto.PermissionTreeDTO;
import com.kk.xhr.model.entity.Permission;
import com.kk.xhr.model.service.IPermissionService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 权限biz服务接口实现
 *
 * @author kkmystery
 * @version 1.0 2021/3/13
 * @since 1.0.0
 */
@Service
@RequiredArgsConstructor
public class PermissionBizServiceImpl implements IPermissionBizService {
    private final IPermissionService permissionService;

    /**
     * {@inheritDoc}
     */
    @Override
    public PermissionTreeVO query(Long id) {
        PermissionTreeDTO dto = permissionService.query(id);
        return this.generateVO(dto);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<Long> queryPermissionByRole(Long roleId) {
        return permissionService.queryByRoleId(roleId);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Long add(PermissionAddRequest request) {
        return permissionService.add(new Permission()
            .withName(request.getName())
            .withTitle(request.getTitle())
            .withComponent(request.getComponent())
            .withIcon(request.getIcon())
            .withPid(request.getPid())
            .withIcon(request.getIcon())
            .withHidden(BinaryEnum.from(request.isHidden()).getValue()));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean modify(Long id, PermissionModifyRequest request) {
        return permissionService.modify(new Permission()
            .withId(id)
            .withName(request.getName())
            .withTitle(request.getTitle())
            .withComponent(request.getComponent())
            .withIcon(request.getIcon())
            .withIcon(request.getIcon())
            .withHidden(BinaryEnum.from(request.isHidden()).getValue()));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean modifyStatus(Long id, BinaryEnum status) {
        return permissionService.modifyStatus(id, status);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void upsertToRole(Long roleId, List<Long> ids) {
        if (ids.isEmpty()) {
            return;
        }
        // 删除角色的权限然后重新添加
        permissionService.deletePermissionByRoleId(roleId);
        permissionService.batchInsert(roleId, ids);
    }

    /**
     * vo生成器
     *
     * @param dto PermissionTreeDTO
     * @return PermissionTreeVO
     */
    private PermissionTreeVO generateVO(PermissionTreeDTO dto) {
        List<PermissionTreeVO> children = new ArrayList<>();
        PermissionTreeVO vo = PermissionTreeVO.builder()
            .id(dto.getId())
            .pid(dto.getPid())
            .name(dto.getName())
            .path(dto.getPath())
            .title(dto.getTitle())
            .component(dto.getComponent())
            .hidden(dto.isHidden())
            .meta(new Meta().setTitle(dto.getTitle()).setIcon(dto.getIcon()))
            .build();
        for (PermissionTreeDTO child : dto.getChildren()) {
            children.add(this.generateVO(child));
        }
        vo.setChildren(children.isEmpty() ? null : children);
        return vo;
    }
}
